package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {

    @Autowired
    private IWorkerCertificationAuditService workerCertificationAuditService;

    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    /**
     * 提交服务人员认证申请
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = DBException.class)
    public void submit(WorkerCertificationAuditAddReqDTO dto) {
        //判断参数
        if (ObjectUtils.isEmpty(dto)) {
            throw new BadRequestException("参数错误");
        }
        //创建存储对象
        WorkerCertification workerCertification = BeanUtils.copyBean(dto, WorkerCertification.class);
        workerCertification.setId(UserContext.currentUserId());
        //设置认证状态
        workerCertification.setCertificationStatus(1);
        boolean save = this.saveOrUpdate(workerCertification);
        if (!save) {
            throw new DBException("添加认证信息失败");
        }
        //同时存入认证审核表
        //构造认证审核对象
        WorkerCertificationAudit workerCertificationAudit = WorkerCertificationAudit.builder()
                .serveProviderId(UserContext.currentUserId())
                .name(dto.getName())
                .idCardNo(dto.getIdCardNo())
                .frontImg(dto.getFrontImg())
                .backImg(dto.getBackImg())
                .certificationMaterial(dto.getCertificationMaterial())
                .auditStatus(0)
                .certificationStatus(1)
                .build();
        boolean save1 = workerCertificationAuditService.save(workerCertificationAudit);
        if (!save1) {
            throw new DBException("添加认证审核信息失败");
        }


    }


    /**
     * 获取驳回原因
     *
     * @return
     */
    @Override
    public Map getRejectReason() {
        //查询审核状态为审核
        //状态为审核失败 3
        //根据审核时间排序
        List<WorkerCertificationAudit> list = workerCertificationAuditService.lambdaQuery()
                .eq(WorkerCertificationAudit::getAuditStatus, 1)
                .eq(WorkerCertificationAudit::getCertificationStatus, 3)
                .eq(WorkerCertificationAudit::getServeProviderId, UserContext.currentUserId())
                .orderByDesc(WorkerCertificationAudit::getAuditTime)
                .list();
        Map<String, String> stringStringMap = new HashMap<>();
        //只要最新的一条
        if (ObjectUtils.isNotEmpty(list)) {
            stringStringMap.put("rejectReason", list.get(0).getRejectReason());

        }
        return stringStringMap;
    }


}
